import com.demon.PublicClient;
import com.demon.SubscribeClient;
import com.demon.SubscribeClient2;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * Created by li on 2016/10/17.
 */
public class Main {

    public static void main(String[] args) throws IOException, TimeoutException {
        System.out.println("建立发布端：请输入：public virtualHost:? userName:? password:? hostName:? portNumber:? exchangeName:? exchangeType:?");
        System.out.println("开始订阅：请输入：subscribe virtualHost:? userName:? password:? hostName:? portNumber:? exchangeName:? exchangeType:? routingKey:? queueName:?");
        System.out.println("开始订阅模式2(不建立exchange)：请输入：subscribe2 virtualHost:? userName:? password:? hostName:? portNumber:? exchangeName:?  routingKey:? queueName:?");
        System.out.println("退出：请输入：exit");
        Scanner sc = new Scanner(System.in);
        SubscribeClient subscribeClient = null;
        SubscribeClient2 subscribeClient2 = null;
        PublicClient publicClient = null;
        while (sc.hasNextLine()) {
            String cmd = sc.nextLine();
            cmd = cmd.trim();
            if (cmd.equalsIgnoreCase("exit")) {
                System.out.println("退出。。。。");
                System.exit(0);
                return;
            }
            String[] cmds = cmd.split("[ \t\r]+");
            for (String i : cmds) {
                System.out.printf("[%s]", i);
            }
            System.out.println();
            if (cmds.length == 0) {
                continue;
            }
            if (subscribeClient != null||subscribeClient2!=null) {
                continue;
            } else if (publicClient != null) {
                cmds = cmd.split("[ \t\r]+");
                if(cmds.length==2) {
                    publicClient.sendMsg(cmds[0], cmds[1]);
                }else{
                    System.out.printf("！错误routingKey message,其中一个不能少");
                }
                System.out.printf("输入routingKey message：");

            } else if (cmds[0].equalsIgnoreCase("public")) {
                System.out.println("发布消息");
                if (cmds.length != 8) {
                    System.out.println("参数不足。。。");
                    continue;
                }
                String virtualHost = cmds[1];
                String userName = cmds[2];
                String password = cmds[3];
                String hostName = cmds[4];
                String portNumber = cmds[5];
                String exchangeName = cmds[6];
                String exchangeType = cmds[7];
                if (!portNumber.matches("^[0-9]+$")) {
                    System.out.printf("portNumber不为整数，输入的是%s", portNumber);
                    continue;
                }
                System.out.printf("开始发布消息,virtualHost:%s,userName:%s,password:%s,hostName:%s,portNumber:%s,exchangeName:%s,exchangeType:%s\n",
                        virtualHost, userName, password, hostName, portNumber, exchangeName,exchangeType);
                System.out.printf("输入routingKey message：");
                publicClient = new PublicClient(virtualHost, userName, password, hostName, Integer.parseInt(portNumber), exchangeName, exchangeType
                        );

            } else if (cmds[0].equalsIgnoreCase("subscribe")) {
                System.out.println("订阅消息...");
                if (cmds.length != 10) {
                    System.out.println("参数不足。。。");
                    continue;
                }
                String virtualHost = cmds[1];
                String userName = cmds[2];
                String password = cmds[3];
                String hostName = cmds[4];
                String portNumber = cmds[5];
                String exchangeName = cmds[6];
                String exchangeType = cmds[7];
                String routingKey = cmds[8];
                String queueName = cmds[9];
                if (!portNumber.matches("^[0-9]+$")) {
                    System.out.printf("portNumber不为整数，输入的是%s", portNumber);
                    continue;
                }
                System.out.printf("开始订阅消息,virtualHost:%s,userName:%s,password:%s,hostName:%s,portNumber:%s,queueName:%s,routingKey:%s\n", virtualHost, userName, password, hostName, portNumber, queueName,routingKey);
                subscribeClient = new SubscribeClient(virtualHost, userName, password, hostName, Integer.parseInt(portNumber), exchangeName,exchangeType,routingKey,queueName);
            }else if (cmds[0].equalsIgnoreCase("subscribe2")) {
                System.out.println("订阅消息...");
                if (cmds.length != 9) {
                    System.out.println("参数不足。。。");
                    continue;
                }
                String virtualHost = cmds[1];
                String userName = cmds[2];
                String password = cmds[3];
                String hostName = cmds[4];
                String portNumber = cmds[5];
                String exchangeName = cmds[6];
                String routingKey = cmds[7];
                String queueName = cmds[8];
                if (!portNumber.matches("^[0-9]+$")) {
                    System.out.printf("portNumber不为整数，输入的是%s", portNumber);
                    continue;
                }
                System.out.printf("开始订阅消息,virtualHost:%s,userName:%s,password:%s,hostName:%s,portNumber:%s,queueName:%s,routingKey:%s\n", virtualHost, userName, password, hostName, portNumber, queueName,routingKey);
                subscribeClient2 = new SubscribeClient2(virtualHost, userName, password, hostName, Integer.parseInt(portNumber), exchangeName,routingKey,queueName);
            }
        }
    }
}
